pipeline {
    agent any
    tools {
        maven "apache-maven-3.6.3"
    }

    stages {
        stage('拉取代码') {
            agent none
            steps {
                git branch: 'master', credentialsId: 'git', url: 'https://gitee.com/ylxupup/mark-parent.git'
            }
        }

        stage('项目编译') {
            agent none
            steps {
                //编译  批量 指定配置文件                                                                     编译子模块
                sh "mvn -B -s /usr/share/maven/conf/settings.xml clean install -T 1C -Dmaven.test.skip=true -am"
            }
        }

        stage('构建-推送-镜像') {
            agent none
            steps {
                script {
                    imagesRoot = "registry-vpc.cn-guangzhou.aliyuncs.com/ylx001/ylx"
                    sh "pwd"
                    sh "cp Dockerfile target/"
                    withDockerRegistry(credentialsId: 'dockerRepository', toolName: 'docker', url: "https://${imagesRoot}") {
                        image = docker.build("${imagesRoot}:v_${BUILD_NUMBER}", "target/")
                        image.push();
                    }
                }
            }
        }

        stage('部署') {
            steps {
                sshCommand remote: [name: 'root', host: '8.134.217.134', user: 'root', password: 'Ylx458040194', allowAnyHosts: true],
                        command: "cd /ylx/webImage && sh image-start.sh ${BUILD_NUMBER}"
            }
        }

        stage('查看镜像版本') {
            agent none
            steps {
                script {
                    imagesRoot = "registry-vpc.cn-guangzhou.aliyuncs.com/ylx001/ylx";
                    echo "打包镜像版本号：v_${BUILD_NUMBER}"
                    echo "镜像全路径：${imagesRoot}:v_${BUILD_NUMBER}"
                }

            }
        }
    }
}